//search_area_list_ctrl
function search_area_list_ctrl()
{
    $("#search_area_list").show();
    return false;
}

function search_area_list_elemt()
{
    $("#search_area_name").val($(this).text());
    var area = $(this).attr("id");
    $("#search_area_list").hide();
    //redirect
    if (area == 0) {
	location.href=cur_url;
	return;
    }
    location.href=cur_url + "area/" + area;
}

//gen_goal_list_ctrl
function gen_goal_list_ctrl()
{
    $("#gen_goal_list").show();
    return false;
}

function gen_goal_list_elemt()
{
    $("#gen_goal_name").val($(this).text());
    var gen_goal = $(this).attr("id");
    $("#gen_goal_list").hide();
    //redirect
    if (gen_goal == 0) {
	location.href=cur_url;
	return;
    }
    location.href="/goal/community/ggid/"+gen_goal;
}


function add_this_goal()
{
    $("#add_this_goal_shadow").click();
}


function nsyc_submit()
{
    var email = $("#nsyc_email").val();
    var area  = $("#nsyc_area").val();
    if (is_email(email) == false) {
        alert("Email is not valid.");
        return false;
    }
    if (area == "" || area.length > 64) {
        alert("area or city is not valid.");
        return false;
    }
    var data = {
        email : email,
        area : area
    };
    $.ajax({
	'type' : 'POST',
	'data' : data,
	'dataType' : 'json',
	'url'  : '/goal/nsyc',
	success : function(msg){
	    if (msg.status == 0) {
            alert("Submit successfully.");
            $.fancybox.close();
	    }
	    else {
            alert(msg.content);
	    }
	},
	error : function(xhr) {
	    alert("Exception");
	}
    
    });
}

$(function(){
    $("#search_area_list_ctrl").click(search_area_list_ctrl);
    $("#search_area_name").click(search_area_list_ctrl);
    $("#search_area_block").mouseleave(function(){$("#search_area_list").hide();});
    $("#search_area_list").find("li").each(function(){$(this).click(search_area_list_elemt)});

    $("#gen_goal_list_ctrl").click(gen_goal_list_ctrl);
    $("#gen_goal_name").click(gen_goal_list_ctrl);
    $("#gen_goal_block").mouseleave(function(){$("#gen_goal_list").hide();});
    $("#gen_goal_list").find("li").each(function(){$(this).click(gen_goal_list_elemt)});
    
    $("#add_this_goal").click(add_this_goal);
    //not see your city
    $("#nsyc_button").click(nsyc_submit);
    //people like me
    $("#people_like_me").click(function(){
	if (is_login() == false) {
	    $("#pop_login_ctrl").click();
	    return false;
	}	
	else {
	    location.href="/goal/search/likeme/1";
	}
    });
});
